package cn.com.jrr.core.service.dictionary;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.jrr.core.common.ToolUtils;
import cn.com.jrr.core.dao.impl.BaseDaoImpl;
import cn.com.jrr.core.model.DataTableRequest;
import cn.com.jrr.core.model.DataTableResponse;
import cn.com.jrr.core.model.PageModel;
import cn.com.jrr.core.model.dictionary.Code;
import cn.com.jrr.core.model.dictionary.CodeType;

@Service(value = "codeTypeService")
public class CodeTypeService {
	@Resource
	private BaseDaoImpl<CodeType> baseDaoImpl;
	@Autowired
	private PageModel<CodeType> pageModel;
	/**
	 * 查询数据
	 * */
	public DataTableResponse<CodeType> getByCode(DataTableRequest dataTableRequest,String code){
		pageModel.setCurrentItem(dataTableRequest.getCurrentItem());
		pageModel.setPageSize(dataTableRequest.getPageSize());
		String sql="select * from codetype where code = ? order by id desc";
		Object [] obj=new String[]{"code","typeCode","typeName"};
		sql=ToolUtils.SearchAndSortHql(sql, dataTableRequest, obj);
		DataTableResponse<CodeType> dataTableResponse=baseDaoImpl.findBySql(sql,pageModel,CodeType.class,new Object[]{code});
		return  dataTableResponse;
	}

	public List<CodeType> list(){
		return baseDaoImpl.findBySql("select * from codetype", CodeType.class);
	}
	public List<CodeType> listByCode(String code){
		return baseDaoImpl.findBySql("select * from codetype where code = ?", CodeType.class,new Object[]{code});
	}
	public int add(CodeType codeType){
		return baseDaoImpl.updateBySql("insert into codetype(typeName,typeCode,code) values(?,?,?)",
				new Object[]{
						codeType.getTypeName(),
						codeType.getTypeCode(),
						codeType.getCode()
				});
	}
	/**
	 * 更新记录
	 * */
	public int update(CodeType codeType){
		return  baseDaoImpl.updateBySql("update codetype set code=?,typeName=?,typeCode=? where id=?", new Object[]{
				codeType.getCode(),codeType.getTypeName(),codeType.getTypeCode(),codeType.getId()});
	}
	public int getByCodeAndType(CodeType codeType){
		List<CodeType> cts=baseDaoImpl.findBySql("select * from codetype where code=? and typeCode=?",CodeType.class, new Object[]{codeType.getCode(),codeType.getTypeCode()});
		return cts.size();
	}
	public CodeType getById(Integer id){
		return baseDaoImpl.get(CodeType.class, id);
	}
	/**
	 * 删除记录
	 * */
	public int delete(Integer id){
		return  baseDaoImpl.updateBySql("delete from codetype where id=?", new Object[]{id});
	}
}
